package com.zq.rabbitmq.config;

import com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {

    @Bean(name = "queue01")
    public Queue queue01(){
        return new Queue("queue01");
    }
    @Bean(name = "queue02")
    public Queue queue02(){
        return new Queue("queue02");
    }
    @Bean(name = "queue03")
    public Queue queue03(){
        return new Queue("queue03");
    }
    @Bean(name = "queue04")
    public Queue queue04(){
        return new Queue("queue04");
    }

    @Bean(name="directEx")
    public DirectExchange directExchange(){
        return new DirectExchange("directEx", false, false);
    }
    @Bean(name = "fanoutEx")
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutEx", false, false);
    }
    @Bean(name = "topicEx")
    public TopicExchange topicExchange(){
        return new TopicExchange("topicEx",false,false);
    }

    //绑定
    @Bean
    public Binding directBinding(){
        return BindingBuilder.bind(queue01()).to(directExchange()).with("queue01");
    }
    @Bean
    public Binding directBinding1(){
        return BindingBuilder.bind(queue02()).to(directExchange()).with("queue02");
    }
    @Bean
    public Binding fanoutBinding(){
        return BindingBuilder.bind(queue03()).to(fanoutExchange());
    }
    @Bean
    public Binding fanoutBinding1(){
        return BindingBuilder.bind(queue04()).to(fanoutExchange());
    }

    @Bean
    public RabbitTemplate rabbitTemplate(){
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }



}
